package com.company.eos.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.company.eos.common.util.PageUtils;
import com.company.eos.controller.form.amecttype.AmectTypeForm;
import com.company.eos.controller.form.amecttype.SearchAmectTypeByIdForm;
import com.company.eos.controller.form.user.DeleteUserByIdsForm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.company.eos.common.R;
import com.company.eos.domain.AmectType;
import com.company.eos.service.AmectTypeService;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author: Song, Wei
 * @date: 2022-03-20
 */
@Slf4j
@RestController
@RequestMapping("/amect_type")
@Tag(name = "罚款类型模块Web接口")
public class AmectTypeController {

    @Autowired
    private AmectTypeService amectTypeService;

    @GetMapping("/searchAllAmectType")
    @Operation(summary = "查询所有罚款类型")
    @SaCheckLogin
    public R searchAllAmectType() {
        log.info("查询所有罚款类型");
        final ArrayList<AmectType> list = amectTypeService.searchAmectType();
        return R.ok().put("list", list);
    }

    @PostMapping("/searchAllType")
    @Operation(summary = "分页查询所有罚款类型")
    @SaCheckLogin
    public R searchAllAmectTypePage(@RequestBody AmectTypeForm form) {
        log.info("分页查询所有罚款类型，参数：{}", form);
        @NotNull(message = "page不能为空") @Min(value = 1, message = "page不能小于1") Integer page = form.getPage();
        @NotNull(message = "length不能为空") @Range(min = 10, max = 50, message = "length必须在10～50之间") Integer length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);

        PageUtils pageUtils = amectTypeService.searchAllAmectType(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/searchAmectPageByType")
    @Operation(summary = "根据条件查询所有罚款类型")
    @SaCheckLogin
    public R searchAmectTypeByType(@RequestBody AmectTypeForm form) {
        log.info("根据条件查询所有罚款类型，参数：{}", form);
        @NotNull(message = "page不能为空") @Min(value = 1, message = "page不能小于1") Integer page = form.getPage();
        @NotNull(message = "length不能为空") @Range(min = 10, max = 50, message = "length必须在10～50之间") Integer length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);

        PageUtils pageUtils = amectTypeService.searchAllAmectType(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/insert")
    @Operation(summary = "添加罚款类型")
    @SaCheckLogin
    public R addAmectType(@RequestBody JSONObject jsonObject) {
        log.info("添加罚款类型，参数：{}", jsonObject);
        String money = jsonObject.getString("money");
        String type = jsonObject.getString("type");
        BigDecimal bigDecimal = new BigDecimal(money);
        return R.ok().put("rows", amectTypeService.insert(bigDecimal, type));
    }

    @PostMapping("/update")
    @Operation(summary = "修改罚款类型")
    @SaCheckLogin
    public R updateAmectType(@RequestBody JSONObject jsonObject) {
        log.info("修改罚款类型，参数：{}", jsonObject);
        String money = jsonObject.getString("money");
        String type = jsonObject.getString("type");
        BigDecimal bigDecimal = new BigDecimal(money);
        return R.ok().put("rows", amectTypeService.update(bigDecimal, type));
    }

    @PostMapping("/delete")
    @Operation(summary = "删除罚款类型")
    @SaCheckLogin
    public R deleteAmectType(@RequestBody DeleteUserByIdsForm form) {
        log.info("删除罚款类型，参数：{}", form);
        @NotEmpty(message = "ids不能为空") Integer[] ids = form.getIds();
        int rows =  amectTypeService.delete(ids);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/searchById")
    @Operation(summary = "修改之前查询罚款类型")
    @SaCheckLogin
    public R selectAmectType(@RequestBody SearchAmectTypeByIdForm form) {
        log.info("修改之前查询罚款类型，参数：{}", form);
        HashMap map = amectTypeService.searchById(form.getId());
        return R.ok(map);
    }



}
